// 导入type和接口api
import { TOKEN_KEY, TOKEN_REMOVE, USER_INFO_SET } from '../actionTypes'
import { sysLogin, sysProfile, sysUser } from '@/api'
import { setToken, removeToken } from '@/utils/auth.js'
import { message } from 'antd'
// 登录的action
export function loginAction(data) {
  return async (dispatch) => {
    // console.log(data)
    const res = await sysLogin(data)
    console.log(res)
    message.success(res.message)
    // 本地保存token
    await setToken(res.data)
    // 仓库保存token
    dispatch({ type: TOKEN_KEY, payload: res.data })
  }
}
// 退出登录
export function logoutAction() {
  return async (dispatch) => {
    await removeToken()
    dispatch({ type: TOKEN_REMOVE })
    dispatch({ type: USER_INFO_SET, payload: {} })
  }
}
// 获取用户信息
export function userinfoAction() {
  return async (dispatch) => {
    // 获取用户基本信息以及详细信息
    const res = await sysProfile()
    const res2 = await sysUser(res.data.userId)
    // console.log(res2)
    // let userinfo={...res.da}
    // console.log(res)
    dispatch({ type: USER_INFO_SET, payload: { ...res.data, ...res2.data } })
  }
}
